﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Country : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string LeagueId;
            string CountryName = "";
            //Add security, if user is not registerd redirect
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string CountryId = null;
            
            if (Request.QueryString["Id"] != null)
                CountryId = Request.QueryString["Id"].ToString();

            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            if (CountryId == null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                CountryId = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetCountryByUser", ou.UserId));
            }
            if (CountryId == null)
                Response.Redirect("~/Default.aspx");
            else if (!IsPostBack)
            {
                this.SqlDataSource1.SelectParameters.Add("CountryId", CountryId);
                this.SqlDataSource1.SelectParameters.Add("Language", ou.Language);
                ManagersDataSource.SelectParameters.Add("Language", ou.Language);
                ManagersDataSource.SelectParameters.Add("CountryId", CountryId);
                this.NationalCupLinkButton.NavigateUrl = "NationalCup.aspx?CountryId=" + CountryId;
                LeagueId = GND.ReadNivdalIntData("@Country", "Nivdal_GetTopLeagueIdByCountry", CountryId);
                this.SqlDataSource2.SelectParameters.Add("LeagueId", LeagueId);
                this.TopLeagueNameLinkButton.NavigateUrl = "~/league.aspx?LeagueId=" + LeagueId;
                this.TopLeagueNameLinkButton.Text = GND.ReadNivdalStringData("@LeagueId", "Nivdal_GetLeagueName", LeagueId);
                CountryName = GND.ReadNivdalStringData("@CountryId", "Nivdal_GetCountryNameByIdProc", CountryId);
                BonusPromotionsInfoHyperlink.NavigateUrl = "BonusPromotions.aspx?LeagueId=" + LeagueId;
                this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Country_PageTitle + ": " + CountryName;
                this.PageNameLabel.Text = Resources.Resource.Global_National + ">> " + CountryName;
                this.JoinedUsersDataSource.SelectParameters.Add("CountryId", CountryId);
                this.OtherScheduleDataSource.SelectParameters.Add("CountryId", CountryId);
                this.OtherScheduleDataSource.SelectParameters.Add("Language", ou.Language);
                this.OnlineUsersDataSource.SelectParameters.Add("CountryId", CountryId);
                this.OnlineUsersDataSource.SelectParameters.Add("Language", ou.Language);
                string NewspaperId = GND.ReadNivdalIntData("Country", "Nivdal_GetNewspaperByCountry", CountryId);
                //Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + CountryName + " .');</script>");
                if (string.IsNullOrEmpty(CountryName))
                    Response.Redirect("~/Default.aspx");
                if (NewspaperId != "0")
                {
                    NewspaperHyperlink.NavigateUrl = "~/Newspaper.aspx?Id=" + NewspaperId;
                }
                else
                {
                    NewspaperHyperlink.Visible = false;
                }
                ElectionsHyperlink.NavigateUrl = "~/Elections.aspx?CountryId=" + CountryId;
            }

        }

        protected override void InitializeCulture()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {

                OnlineUser ou = (OnlineUser)Session["User"];
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
            }
        }
    }
}